//
//  UIView+SCBaseExt.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/17.
//

#import "UIView+SCBaseExt.h"
#import <objc/runtime.h>
#import <Masonry/Masonry.h>
#import "SCBasicKit.h"

@implementation UIView (SCBaseVC)

- (UIViewController *)sc_vc {
    UIResponder *next = [self nextResponder];
    while (next != nil) {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        } else {
            next = [next nextResponder];
        }
    }
    return nil;
}

@end

@implementation UIView (SCViewEffect)

- (void)sc_BlurEffect {
    UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

    visualEffectView.alpha = 1;//模糊程度

    [self addSubview:visualEffectView];
    
    [visualEffectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)sc_Shadow {
    self.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.1].CGColor;
    self.layer.shadowRadius = 3;
    self.layer.shadowOpacity = 1;
    self.layer.shadowOffset = CGSizeMake(2, 2);
}

- (void)sc_roundingCorner:(UIRectCorner)corner inRectSize:(CGSize)rectSize cornerRadius:(CGFloat)radius {
    CGRect rect = CGRectMake(0, 0, rectSize.width, rectSize.height);
    CGSize radii = CGSizeMake(radius, radius);
    [self sc_roundingCorner:corner inRect:rect cornerRadii:radii];
}

- (void)sc_roundingCorner:(UIRectCorner)corner inRect:(CGRect)rect cornerRadii:(CGSize)radii {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radii];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = rect;
    shapeLayer.path = maskPath.CGPath;
    self.layer.mask = shapeLayer;
}

- (void)sc_gradientStyleColors:(NSArray *)colors Position:(SCGradientPosition)position {
    [self sc_gradientStyleColors:colors Position:position Size:self.bounds.size];
}

- (void)sc_gradientStyleColors:(NSArray *)colors Position:(SCGradientPosition)position Size:(CGSize)size {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    NSMutableArray *cgColors = @[].mutableCopy;
    for(UIColor *color in colors) {
        [cgColors addObject:(id)color.CGColor];
    }
    gradientLayer.colors = cgColors;
    gradientLayer.locations = @[@0.0,@1.0];
    if (position == SCGradientPositionLeftToRight) {
        gradientLayer.startPoint = CGPointMake(0, 0.5);
        gradientLayer.endPoint = CGPointMake(1, 0.5);
    } else if (position == SCGradientPositionRightToLeft) {
        gradientLayer.startPoint = CGPointMake(1, 0.5);
        gradientLayer.endPoint = CGPointMake(0, 0.5);
    } else if (position == SCGradientPositionBottomToTop) {
        gradientLayer.startPoint = CGPointMake(0.5, 1);
        gradientLayer.endPoint = CGPointMake(0.5, 0);
    } else {
        gradientLayer.startPoint = CGPointMake(0.5, 0);
        gradientLayer.endPoint = CGPointMake(0.5, 1);
    }
    
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    [self.layer addSublayer:gradientLayer];
}

@end

@implementation UIView (SCBaseExt)

- (CGFloat)sc_x {
    return self.frame.origin.x;
}

- (void)setSc_x:(CGFloat)sc_x {
    CGRect newframe = self.frame;
    newframe.origin.x = sc_x;
    self.frame = newframe;
}

- (CGFloat)sc_y {
    return self.frame.origin.y;
}

- (void)setSc_y:(CGFloat)sc_y {
    CGRect newframe = self.frame;
    newframe.origin.y = sc_y;
    self.frame = newframe;
}

- (CGFloat)sc_width {
    return self.frame.size.width;
}

- (void)setSc_width:(CGFloat)sc_width {
    CGRect newframe = self.frame;
    newframe.size.width = sc_width;
    self.frame = newframe;
}

- (CGFloat)sc_height {
    return self.frame.size.height;
}

- (void)setSc_height:(CGFloat)sc_height {
    CGRect newframe = self.frame;
    newframe.size.height = sc_height;
    self.frame = newframe;
}

- (CGFloat)sc_left {
    return self.frame.origin.x;
}

- (void)setSc_left:(CGFloat)sc_left {
    CGRect newframe = self.frame;
    newframe.origin.x = sc_left;
    self.frame = newframe;
}

- (CGFloat)sc_top {
    return self.frame.origin.y;
}

- (void)setSc_top:(CGFloat)sc_top {
    CGRect newframe = self.frame;
    newframe.origin.y = sc_top;
    self.frame = newframe;
}

- (CGFloat)sc_right {
    return self.frame.size.width + self.frame.origin.x;
}

-(void)setSc_right:(CGFloat)sc_right {
    CGRect newframe = self.frame;
    newframe.size.width = sc_right - self.frame.origin.x;
    self.frame = newframe;
}

- (CGFloat)sc_bottom {
    return self.frame.size.height + self.frame.origin.y;
}

- (void)setSc_bottom:(CGFloat)sc_bottom {
    CGRect newframe = self.frame;
    newframe.size.height = sc_bottom = self.frame.origin.y;
    self.frame = newframe;
}

@end

@implementation UIView (SCBasePlaceholder)
#pragma mark - property
- (UIView *)sc_showBgView {
    UIView *bgView = objc_getAssociatedObject(self, @"sc_showBgView");
    if(!bgView) {
        bgView = [[UIView alloc] init];
        objc_setAssociatedObject(self, @"sc_showBgView", bgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return bgView;
}

- (UIImageView *)sc_showImageView {
    UIImageView *imageView = objc_getAssociatedObject(self, @"sc_showImageView");
    if(!imageView) {
        imageView = [[UIImageView alloc] init];
        objc_setAssociatedObject(self, @"sc_showImageView", imageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return imageView;
}

- (UILabel *)sc_showLabel {
    UILabel *label = objc_getAssociatedObject(self, @"sc_showLabel");
    if(!label) {
        label = [[UILabel alloc] init];
        label.font = sc_PingFangRegularFont(13);
        label.textColor = sc_colorHex(0x999999);
        objc_setAssociatedObject(self, @"sc_showLabel", label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return label;
}

- (UIButton *)sc_showBtn {
    UIButton *btn = objc_getAssociatedObject(self, @"scloadBtn");
    if(!btn) {
        btn = [[UIButton alloc] init];
        btn.layer.cornerRadius = 20;
        btn.layer.borderWidth = 1;
        btn.layer.masksToBounds = true;
        btn.layer.borderColor = sc_rgba(229, 229, 229, 1).CGColor;
        btn.titleLabel.font = sc_PingFangRegularFont(14);
        [btn setTitleColor:sc_rgba(102, 102, 102, 1) forState:UIControlStateNormal];
        [btn setTitle:@"重新加载" forState:UIControlStateNormal];
        __weak typeof(self) weakSelf = self;
        [btn addTarget:self action:[self sc_selecorBlock:^(id sender) {
            if(weakSelf.scReloadCallBack) {
                weakSelf.scReloadCallBack();
            }
        }] forControlEvents:UIControlEventTouchUpInside];
        objc_setAssociatedObject(self, @"scloadBtn", btn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return btn;
}

- (void (^)(void))scReloadCallBack {
    return objc_getAssociatedObject(self, @"scReloadCallBack");
}

- (void)setScReloadCallBack:(void (^)(void))scReloadCallBack {
    objc_setAssociatedObject(self, @"scReloadCallBack", scReloadCallBack, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma mark - public
- (void)sc_showPlaceholderType:(SCShowType)showtype {
    [self sc_setupUI];
    
    switch (showtype) {
        case SCShowNone: {
            self.sc_showBgView.hidden = true;
        }
            break;
        case SCShowLoading: {
            self.sc_showLabel.text = @"数据加载中...";
            self.sc_showImageView.image = nil;
            [self.sc_showImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@109);
                make.height.equalTo(@110);
            }];
            NSMutableArray *images = @[].mutableCopy;
            for (int i = 0; i < 40; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%d",i+1]];
                [images addObject:image];
            }
            self.sc_showImageView.animationImages = images;
            self.sc_showImageView.animationDuration = 1.3f;
            [self.sc_showImageView startAnimating];
        }
            break;
        case SCShowApiError: {
            self.sc_showImageView.image = [UIImage imageNamed:@"sc-接口问题"];
            [self.sc_showImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(self.sc_showImageView.image.size.width));
                make.height.equalTo(@(self.sc_showImageView.image.size.height));
            }];
            self.sc_showLabel.text = @"服务器开了会小差，请稍后重试";
        }
            break;
        case SCShowNoData: {
            self.sc_showImageView.image = [UIImage imageNamed:@"sc-暂无数据"];
            [self.sc_showImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(self.sc_showImageView.image.size.width*1.3));
                make.height.equalTo(@(self.sc_showImageView.image.size.height*1.3));
            }];
            [self.sc_showLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.sc_showImageView.mas_bottom).offset(0);
            }];
            self.sc_showLabel.text = @"暂无数据~";
        }
            break;
        case SCShowNoDataOnlyImage: {
            self.sc_showImageView.image = [UIImage imageNamed:@"sc-暂无试卷"];
            [self.sc_showImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(self.sc_showImageView.image.size.width));
                make.height.equalTo(@(self.sc_showImageView.image.size.height));
            }];
            self.sc_showLabel.text = @"";
        }
            break;
            
        case SCShowTokenInvalid: {
            
        }
            break;
            
        case SCShowNoDataBySecure: {
            self.sc_showImageView.image = [UIImage imageNamed:@"sc-无密保"];
            [self.sc_showImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(@(self.sc_showImageView.image.size.width));
                make.height.equalTo(@(self.sc_showImageView.image.size.height));
                make.centerY.equalTo(self.sc_showBgView).offset(-100);
            }];
            self.sc_showLabel.text = @"暂无密保问题~";
            self.sc_showBtn.hidden = NO;
            [self.sc_showBtn setTitle:@"去设置" forState:UIControlStateNormal];
            [self.sc_showBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
 
            UIImage *img = [UIImage sc_imageGradientStyleColors:@[sc_colorHex(0x28c1ff),sc_colorHex(0x198bff)] Position:SCGradientPositionLeftToRight Size:CGSizeMake(120, 40)];
            [self.sc_showBtn setBackgroundImage:img forState:UIControlStateNormal];
            self.sc_showBtn.titleLabel.font = sc_PingFangRegularFont(16);
        }

        default:
            break;
    }
}


- (void)sc_showPlaceholderType:(SCShowType)loadType iconName:(NSString *)iconName title:(NSString *)title {
    
}


- (void)sc_setupUI {
    /// 移动旧页面
    [self.sc_showBgView removeFromSuperview];
    [self.sc_showBtn removeFromSuperview];
    [self.sc_showLabel removeFromSuperview];
    [self.sc_showImageView removeFromSuperview];
    
    [self addSubview:self.sc_showBgView];
    self.sc_showBgView.hidden = false;
    [self.sc_showBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(self.sc_width));
        make.height.equalTo(@(self.sc_height));
        make.center.equalTo(self);
//        make.left.top.bottom.right.equalTo(@0);
    }];
    
    [self.sc_showBgView addSubview:self.sc_showImageView];
    [self.sc_showImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.sc_showBgView);
        make.centerY.equalTo(self.sc_showBgView).offset(-50);
    }];
    
    [self.sc_showBgView addSubview:self.sc_showLabel];
    [self.sc_showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.sc_showBgView);
        make.top.equalTo(self.sc_showImageView.mas_bottom).offset(10);
    }];
    
    [self.sc_showBgView addSubview:self.sc_showBtn];
    self.sc_showBtn.hidden = true;
    [self.sc_showBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.sc_showBgView);
        make.top.equalTo(self.sc_showLabel.mas_bottom).offset(20);
        make.width.equalTo(@(120));
        make.height.equalTo(@(40));
    }];
}

@end
